const t = getApp()
let objectArraySort = function (keyName) {
  return function (objectN, objectM) {
   let valueN = objectN[keyName]
   let valueM = objectM[keyName]
   if (valueN < valueM) return 1
   else if (valueN > valueM) return -1
   else return 0
  }
}
Page({
  data: {
    change:0,
    choosed_money:null,
    InvestList:null,
    balance:0,
    title:'',
    tabIndex:0,
    log:[],
    store_id:null,
    vipList:null,
    myCard:null,
    change:0,
    page:1,
    total:0,
    canpre:false,
    cannext:false,
    log:[],
    wash_log:[],
    navHeight:t.globalData.navHeight,
    title:'充值',
    card_id:"",
  },
  // 生命周期
  // 页面加载的时候执行一次
  onLoad(e){
    let card_id = '';
    if(e.card_id != undefined){
      console.log(e)
      card_id=e.card_id
    }                       
    this.setData({
      store_id:e.store_id,
      card_id
    })
  },
  onShow(e){
    this.getlist()
    this.getInvestList()
    this.getList_new()
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.getlist()
    this.getInvestList()
    this.getList_new()
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  getlist(e){
    var that = this
    var loc = t.globalData.loc
    t.request('/store/index_list',{lat:loc?loc.latitude:0,lng:loc?loc.longitude:0,page:1},function(e){
      if (e.code!==200) return wx.showToast({ title: e.msg,icon:'none' })
      if (e.result.total>0) {
        let list = e.result.list
        list.forEach(ele => {
          ele.distance = ele.distance.toFixed(1)
        });
        that.setData({
          list,loc
        })
      }
    })
  },
  getInvestList(e){
    const that = this
    t.request('/RechargeConfig/getList',{store_id:that.data.store_id,card_id:that.data.card_id},function(e){
      if (e.code!==200) return wx.showToast({ title: e.msg,icon:'none' })
      let list = e.result.config.list
      let log = e.result.log
      let wash_log = e.result.wash_log
      // list.sort(objectArraySort('sort'))
      // console.log(list);
      that.setData({
        InvestList:list,
        balance:e.result.balance,
        title:e.result.title,
        log,
        wash_log
      })
    })
  },
  getList_new(e){
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    let change=that.data.change;
    if(change==1){
      t.request('/users/Washing_log',{page:this.data.page},function(res){
        if (res.code==200) {
         console.log(res);
         let cannext=false;
         if(res.result.total>that.data.page){
          cannext=true;
        }
         that.setData({
          wash_log:res.result.data,
          total:res.result.total,
          cannext:cannext
        })
        wx.hideLoading({
          success: (res) => {},
        })
        }
      })
    }else{
      t.request('/users/Recharge_log',{page:this.data.page},function(res){
        if (res.code==200) {
         console.log(res);
         let cannext=false;
         if(res.result.total>that.data.page){
          cannext=true;
        }
         that.setData({
          log:res.result.data,
          total:res.result.total,
          cannext:cannext
        })
        wx.hideLoading({
          success: (res) => {},
        })
        }
      })
    }
    
  },
  getVip(e){
    const that = this
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    t.request('/RechargeConfig/getVip',{store_id:that.data.store_id},function(e){
      console.log(e);
      wx.hideLoading({
        success: (res) => {},
      })
      if (e.code!==200) return wx.showToast({ title: e.msg,icon:'none' })
      let list = e.result.cardlist.list
      list.sort(objectArraySort('sort'))
      that.setData({
        vipList:list,
        myCard:e.result.myinfo||null
      })
    })
  },
  tab(e){
    this.setData({
      tabIndex:e.target.dataset.index
    })
    e.target.dataset.index == 1 ? this.getVip() : ''
  },
  // 充值
  choose_money(e){
    wx.showLoading({
      title: '请稍后',
    })
    var that = this
    var o = {
      card_id:that.data.card_id,
      store_id:that.data.store_id,
      config:e.currentTarget.dataset.id
    }
    // 调充值接口
    t.request('/Recharge',o,function(e){
      console.log(e);
      if (e.code==200) {
        var params = e.result.params
        wx.requestPayment({
          nonceStr: params.nonceStr,
          package: params.package,
          paySign: params.paySign,
          signType: 'MD5',
          timeStamp: params.timeStamp,
          'success'(e){
            console.log(e);
            wx.hideLoading({
              success: (res) => {},
            })
            wx.navigateBack({
              delta: 0,
            })
          },
          'fail'(e){
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showModal({
              title: '您取消了支付！',
              showCancel:false,
              complete:function(){
              }
            })
          },
        })
      }else{
        wx.showToast({
          title: e.msg,
          icon: 'none',
          duration: 2000
        })
        return false;
      }
    })
    this.setData({
      choosed_money:e.currentTarget.dataset.id
    })
  },
  // 买会员卡
  buy_vip(e){
    const that = this
    const o = {
      store_id:that.data.store_id,
      id:e.currentTarget.dataset.id
    }
    wx.showLoading({ title: '请稍后' })
    t.request('/recharge/vip',o,function(e){
      console.log(e);
      if (e.code!==200) return wx.showToast({ title: e.msg,icon:'none' })
      var params = e.result.params
      wx.requestPayment({
        nonceStr: params.nonceStr,
        package: params.package,
        paySign: params.paySign,
        signType: 'MD5',
        timeStamp: params.timeStamp,
        'success'(e){
          console.log(e);
          wx.hideLoading({
            success: (res) => {},
          })
          that.getVip()
        },
        'fail'(e){
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showModal({
            title: '您取消了支付！',
            showCancel:false,
            complete:function(){
            }
          })
        },
      })
    })
    this.setData({
      choosed_card:e.currentTarget.dataset.id
    })
  },
  change_page(e){
    console.log(e);
    this.setData({
      change:e.currentTarget.dataset.index,
    })

  },
  change_page2(e){
    this.setData({
      change:e.currentTarget.dataset.index,
      page:1
    })
    this.getList_new();
  },
  gopre(){
    this.setData({
      page:this.data.page-1
    })
    this.getList();
  },
  gonext(){
    this.setData({
      page:this.data.page+1
    })
    this.getList();
  },
  navto(e){
    wx.navigateTo({
      url: '/pages/article/article?id=334',
    })
  }
})